<template>
  <div>
    <ContractWord
      :baseInfo="state.formData.Main"
      :subList="state.formData.SubList2"
      :tremZF="state.formData.TremZF"
      :tremJF="state.formData.TremJF"
      :tremQA="state.formData.TremQA"
      :tremYS="state.formData.TremYS"
      :tremD="state.formData.TremD"
      :ContractFile="state.formData.ContractFile"
      :ContractFileChecked="state.formData.ContractFileChecked"
      :tremOther="state.formData.TremOther"
    />
  </div>
</template>

<script setup lang="tsx">
import {onMounted, reactive, watch} from 'vue'
import { getContractInfo } from '@/api/base'
import ContractWord from './components/contract_word.vue'
import { useRoute } from 'vue-router'

const handlEedit = async (id) => {
  //调用查询合同详细信息
  let res = await getContractInfo({ id: id })

  if (res.Code === 'OK') {
    state.formData = res.Data
    if (state.formData.Main.PhasedFile) {
      //转换为数组
      state.formData.Main.PhasedFile = state.formData.Main.PhasedFile.split(',')
    } else {
      state.formData.Main.PhasedFile = []
    }
    if (state.formData.Main.TDYSJDFile){
      //转换为数组
      state.formData.Main.TDYSJDFile = state.formData.Main.TDYSJDFile.split(",")
    }else {
      state.formData.Main.TDYSJDFile = []
    }
    if (state.formData.Main.JHYSJDFile){
      //转换为数组
      state.formData.Main.JHYSJDFile = state.formData.Main.JHYSJDFile.split(",")
    }else {
      state.formData.Main.JHYSJDFile = []
    }

    if (state.formData.Main.JSJDFile){
      //转换为数组
      state.formData.Main.JSJDFile = state.formData.Main.JSJDFile.split(",")
    }else {
      state.formData.Main.JSJDFile = []
    }
    state.isShowSubBut = true
    state.dialogSelectEnt = true
    state.title = '修改合同录入'
  }
}

const handleDelete = (row: any) => {

  state.clumTable.data.splice(state.clumTable.data.indexOf(row), 1)
}

//表格配置
const state: any = reactive({
  queryData: {
    ContractType: null, //合同类型
    CustCode: null, //客户编码
    CustName: null, //客户名称
    ContractNum: null, //合同号
    CreateFrom: null,
    CreateTo: null,
    Type: null,
    CreateBy: null, //建单人
    IsMoudel: null,
    IsChange: true
  },

  loading: false,
  isShowSubBut: true,
  title: '新增合同录入',
  dialogSelectEnt: false,
  //表单
  formData: {
    Main: {},
    SubList: [],
    TremZF: [],
    TremTO: [],
    TremTC: [],
    TremQA: [],
    TremFM: [],
    TremDR: [],
    TremD: [],
    TremOther: [],
    TremYS: [],
    TremBM: [],
    TremJF: [],
    TremBZ: [],
    ContractFile: [],
    Type: 'Pass'
  },
  coseData: [],

  clumTable: {
    border: true,
    firstColumn: { type: 'selection' },
    // 接口返回数据
    data: [],
    // 表头数据
    columns: [
      {
        prop: 'CinvCode',
        label: '设备编码'
      },
      {
        prop: 'CinvName',
        label: '设备名称'
      },
      {
        prop: 'CinvAdd',
        label: '规格型号'
      },
      // {
      //   prop: "Price",
      //   label: "单价",
      //   canEdit: true,
      //   configEdit: {
      //     label: "数量",
      //     type: "input",
      //     editComponent: "el-input"
      //   }
      // },
      {
        prop: 'Qty',
        label: '数量',
        canEdit: true,
        configEdit: {
          label: '数量',
          type: 'input',
          editComponent: 'el-input'
        }
      },
      // {
      //   prop: "Amount",
      //   label: "金额",
      //   canEdit: true,
      //   configEdit: {
      //     label: "数量",
      //     type: "input",
      //     editComponent: "el-input"
      //   }
      // },
      {
        prop: 'Unit',
        label: '单位'
      }
    ],
    listTypeInfo: {
      furnaceLayerList: [
        {
          label: '上层',
          key: '1'
        },
        {
          label: '中层',
          key: '2'
        },
        {
          label: '下层',
          key: '3'
        }
      ]
    },
  },
  editTable: {
    border: true,
    firstColumn: { type: 'selection' },
    // 接口返回数据
    data: [],
    // 表头数据
    columns: [
      {
        prop: 'CinvCode',
        label: '设备编码'
      },
      {
        prop: 'CinvName',
        label: '设备名称'
      },
      {
        prop: 'CinvAdd',
        label: '规格型号'
      },
      {
        prop: 'Quantity',
        label: '数量'
      },
      {
        prop: 'Unit',
        label: '单位'
      }
    ],
    listTypeInfo: {
      furnaceLayerList: [
        {
          label: '上层',
          key: '1'
        },
        {
          label: '中层',
          key: '2'
        },
        {
          label: '下层',
          key: '3'
        }
      ]
    },
    operator: [
      {
        text: '删除',
        fun: handleDelete
      }
    ],
    // 操作列样式
    operatorConfig: {
      fixed: 'right', // 固定列表右边（left则固定在左边）
      width: 200,
      label: '操作'
    }
  },

  rules: {
    ContractNum: [{ required: true, message: '请输入合同编号', trigger: 'blur' }],
    ProjectName: [{ required: true, message: '请输入项目名称', trigger: 'blur' }],
    CustCode: [{ required: true, message: '请选择客户', trigger: 'change' }],
    ContractType: [{ required: true, message: '请选择合同类型', trigger: 'change' }],
    PSType: [{ required: true, message: '请输入评审类型', trigger: 'blur' }],
    Payment: [{ required: true, message: '请选择支付款项', trigger: 'change' }],
    BankCode: [{ required: true, message: '请输入开户行', trigger: 'blur' }],
    BankAccount: [{ required: true, message: '请输入银行账号', trigger: 'blur' }],
    Tax: [{ required: true, message: '请输入税号', trigger: 'blur' }],
    IsCompliance: [{ required: true, message: '请选择是否合规', trigger: 'change' }],
    DeliveryDate: [{ required: true, message: '请选择交付时间', trigger: 'change' }],
    DeliveryLocation: [{ required: true, message: '请输入交付地点', trigger: 'blur' }],
    Rate: [{ required: true, message: '请输入税率', trigger: 'blur' }],
    Unload: [{ required: true, message: '请选择卸货方式', trigger: 'change' }],
    PackRequirement: [{ required: true, message: '请输入包装要求', trigger: 'blur' }],
    ContractAmount: [{ required: true, message: '请输入合同金额', trigger: 'blur' }],
    UnloadConvention: [{ required: true, message: '请输入补充说明', trigger: 'blur' }],
    SigningLocation: [{ required: true, message: '请输入签订地点', trigger: 'blur' }],
    IsMoudel: [{ required: true, message: '请选择合同是否标准', trigger: 'change' }],
    Ddate: [{ required: true, message: '请选择签订日期', trigger: 'blur' }],
    DDate2: [{ required: true, message: '请选择单据日期', trigger: 'blur' }],
    JobNum: [{ required: true, message: '工作号', trigger: 'blur' }],
    SaleNum: [{ required: true, message: '售前号', trigger: 'blur' }]
  },

  ids: []
})

//路由取参
const route = useRoute()

const props = defineProps({
  baseInfo: {
    type: String,
    default: ''
  }
})

//监听baseInfo拷贝至formData
watch(
  () => props.baseInfo,
  (newValue, oldValue) => {
    state.formData = newValue
    console.log(newValue)
  }
)



//页面加载查询后端数据
onMounted(() => {
  setTimeout(() => {
    // console.log(props.baseInfo.Main.Id,"1212")
    if(route.query.id || props.baseInfo.Main.Id)
      handlEedit(route.query.id || props.baseInfo.Main.Id)
  },1000)

})
</script>
